//
//  DMHomeViewCellModel.swift
//  DM_新浪微博
//
//  Created by Damon.JiAo on 16/3/1.
//  Copyright © 2016年 Demon_JiAo. All rights reserved.
//

import UIKit

class DMHomeViewCellModel: NSObject {

    var cellModel :DMStatusModel?
    
    var verifity_image:UIImage?{
        
        switch cellModel?.user?.verified_type ?? -1{
         
        case -1 :return nil
        case 0:return UIImage(named: "avatar_vip")
        case 2,3,5:return UIImage(named: "avatar_enterprise_vip")
        case 220:return UIImage(named: "avatar_grassroot")
            //  必须要加default
        default: return nil
        }
    }
    
    var level_image:UIImage?{
        
        return UIImage(named: "common_icon_membership_level\(cellModel?.user?.mbrank ?? 0)")
        
    }
    
    /** 转发按钮文字 */
    var retweetString:String?{
        
        if cellModel?.reposts_count == 0{
            return "转发"
        }else{
            if let str = cellModel?.reposts_count{
                return "\(str)"
            }
            return "转发"
        }
    }
    
    /// 微博来源返回
    func dealSource(sourceString:String?) -> String?{
        
        guard let source = sourceString else{
            return nil
        }
        
        let sourceArray = source.componentsSeparatedByString(">")
        //
        if sourceArray.count >= 2{
            
            let subSource = sourceArray[1]
            
            let lastSource = subSource.componentsSeparatedByString("<")
            
            if lastSource.count >= 1{
                return lastSource.first
            }else{
                return nil
            }
        }else{
            return nil
        }
        
    }
    
    /// 微博发表时间
    func dealTime(create_date:String?) -> String?{
        
        guard let creatDate = create_date else {
            return nil
        }
        
        let formatter = NSDateFormatter()
        
        formatter.locale = NSLocale(localeIdentifier: "cn")
        
        formatter.dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
        
        let date = formatter.dateFromString(creatDate)
        
        let calendar = NSCalendar.currentCalendar()
        
        let unites = NSCalendarUnit(arrayLiteral: .Year,.Month,.Day,.Hour,.Minute)
        
        //  比较方法
        if let Odate = date{
            
            let compts = calendar.components(unites, fromDate: Odate, toDate: NSDate(), options: [])
            // 2016-03-03 17:23:10 +0000 发布微博的时间
            // 2016-03-03 17:27:10 +0000 现在的时间
            // 现在的时间 - 发布微博的时间
            // 4.通过 返回的值,就可以获取比较的大小
            printLog(compts.year)
            printLog(compts.month)
            printLog(compts.day)
            
            printLog(compts.hour)
            printLog(compts.minute)
        if compts.year == 0 {
            //今年
            if compts.month == 0 { // ==0 是这个月
                
                if compts.day == 0{ // 今天
                    
                    if compts.hour == 0{ // 1小时之内
                        
                        if compts.minute == 0{ // 刚刚
                            
                            return "刚刚"
                        }else { // 几分钟之前
                            return "\(compts.minute)分钟前"
                        }
                        
                    } else { // 1小时之外
                        return "\(compts.hour)小时前"
                    }
                    
                } else if compts.day == 1 { // 昨天
                    // 几点几分  2016-03-04 09:50:27
                    
                    
                    formatter.dateFormat = "HH:mm"
                    let str = formatter.stringFromDate(Odate)
                    return "昨天 \(str)"
                }
                
            } else {
                // 其他月 "MM-dd HH:mm"
                formatter.dateFormat = "MM-dd HH:mm"
                let str = formatter.stringFromDate(Odate)
                return "昨天 \(str)"
            }
        } else {
            //其他年 "MM-dd HH:mm"
            formatter.dateFormat = "MM-dd HH:mm"
            let str = formatter.stringFromDate(Odate)
            return "昨天 \(str)"
        }
      }
        return "\(date)"
    }


    /// 评论
    var commentString:String?

    /// 转发微博内容
    var retweetStatusString:String?{
        
        return cellModel?.retweeted_status?.text
    }
    
}
